//////////////////////////
// Render User Interface
//
//
//
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Helper;
using SpaceWolf.Shaders;
using SpaceWolf.GameLogic;

namespace SpaceWolf.Graphics
{
    public class UIRenderer 
    {
        public int DashBoardX = BaseGame.Width / 2 - 512 / 2;
        public int DashBoardY= BaseGame.Height - 129;
        
        int crossChairX=BaseGame.Width / 2 - 48;
        int crossChairY=BaseGame.Height / 2 - 41;
        /// <summary>
        /// Pre screen sky cube mapping shader for the space background
        /// </summary>
        public static PreScreenSkyCubeMapping skyCube = null;

        LensFlare lensFlare = null;

        Texture background;
        Texture SCU_Texture;

        Texture optionsScreen;
        Texture InSpaceUI;

        public Texture OptionsScreen
        {
            get
            {
                return this.optionsScreen;
            }
        }
        /// <summary>
        /// Buttons
        /// </summary>
        /// <summary>
        /// Mouse cursor
        /// </summary>
        Texture mouseCursor;
       
        /// <summary>
        /// Font for rendering text
        /// </summary>
        Fonts font = new Fonts();

        Texture MenuTexture;

        #region Constructor
        /// <summary>
        /// Create user interface renderer
        /// </summary>
        public UIRenderer()
        {
            optionsScreen = new Texture("OptionsScreen.png");
            MenuTexture = new Texture("menu");
            background = new Texture("screens/Loading_Screen");
            SCU_Texture = new Texture("screens/SCU_Screen");
            mouseCursor = new Texture("MouseCursor.png");
            InSpaceUI = new Texture("InSpaceUI.png");
            //ShipDashboard = new Texture("SCANNERV");
            //CrossChair = new Texture("CrossChair");
            //DockingCompOn = new Texture("AllowDocking");

            skyCube = new PreScreenSkyCubeMapping();

            BaseGame.LightDirection = new Vector3(0, 100, 1);

            font = Fonts.LoadFont(BaseGame.Device, "fonts/aston.xml");

            lensFlare = new LensFlare(LensFlare.DefaultSunPos);

        } // UIRenderer()

        public void RenderGameUI()
        {
            background.RenderOnScreen(new Rectangle(0, 0,BaseGame.Width,BaseGame.Height));
            // Commander
            MenuTexture.RenderOnScreen(new Rectangle(10, BaseGame.Height-73, 64,64),new Rectangle(0,0,64,64));
            // Market
            MenuTexture.RenderOnScreen(new Rectangle(75, BaseGame.Height - 73, 64, 64), new Rectangle(65, 0, 64, 64));
            // Map
            MenuTexture.RenderOnScreen(new Rectangle(140, BaseGame.Height - 73, 64, 64), new Rectangle(130, 0, 64, 64));
            // Armory
            MenuTexture.RenderOnScreen(new Rectangle(205, BaseGame.Height - 73, 64, 64), new Rectangle(195, 0, 64, 64));
            // Ships
            MenuTexture.RenderOnScreen(new Rectangle(270, BaseGame.Height - 73, 64, 64), new Rectangle(260, 0, 64, 64));
            // Missions
            MenuTexture.RenderOnScreen(new Rectangle(335, BaseGame.Height - 73, 64, 64), new Rectangle(325, 0, 64, 64));
            // Planet
            MenuTexture.RenderOnScreen(new Rectangle(400, BaseGame.Height - 73, 64, 64), new Rectangle(390, 0, 64, 64));
            // Journal
            MenuTexture.RenderOnScreen(new Rectangle(465, BaseGame.Height - 73, 64, 64), new Rectangle(455, 0, 64, 64));
            // Options
            MenuTexture.RenderOnScreen(new Rectangle(530, BaseGame.Height - 73, 64, 64), new Rectangle(520, 0, 64, 64));

        }

        public void RenderGameUI(bool i)
        {
            SCU_Texture.RenderOnScreen(new Rectangle(0, 0, BaseGame.Width, BaseGame.Height));
            // Commander
            MenuTexture.RenderOnScreen(new Rectangle(10, BaseGame.Height - 73, 64, 64), new Rectangle(0, 0, 64, 64));
            // Market
            MenuTexture.RenderOnScreen(new Rectangle(75, BaseGame.Height - 73, 64, 64), new Rectangle(65, 0, 64, 64));
            // Map
            MenuTexture.RenderOnScreen(new Rectangle(140, BaseGame.Height - 73, 64, 64), new Rectangle(130, 0, 64, 64));
            // Armory
            MenuTexture.RenderOnScreen(new Rectangle(205, BaseGame.Height - 73, 64, 64), new Rectangle(195, 0, 64, 64));
            // Ships
            MenuTexture.RenderOnScreen(new Rectangle(270, BaseGame.Height - 73, 64, 64), new Rectangle(260, 0, 64, 64));
            // Missions
            MenuTexture.RenderOnScreen(new Rectangle(335, BaseGame.Height - 73, 64, 64), new Rectangle(325, 0, 64, 64));
            // Planet
            MenuTexture.RenderOnScreen(new Rectangle(400, BaseGame.Height - 73, 64, 64), new Rectangle(390, 0, 64, 64));
            // Journal
            MenuTexture.RenderOnScreen(new Rectangle(465, BaseGame.Height - 73, 64, 64), new Rectangle(455, 0, 64, 64));
            // Options
            MenuTexture.RenderOnScreen(new Rectangle(530, BaseGame.Height - 73, 64, 64), new Rectangle(520, 0, 64, 64));

        }


        public void RenderSpaceUI(Ship CommanderShip)
        {
            

            skyCube.RenderSky();
            lensFlare.Render(Microsoft.Xna.Framework.Graphics.Color.White);
            
            if (Camera.currentCameraMode == CameraMode.FirstPerson)
            {
                // CrossChair
                InSpaceUI.RenderOnScreen(new Rectangle(crossChairX, crossChairY, 96, 81), new Rectangle(0, 0, 96, 81));
                
                InSpaceUI.RenderOnScreen(new Rectangle(DashBoardX, DashBoardY, 512, 129), new Rectangle(0, 82,512, 129));

                // Laser temperature
                display_dial_bar(CommanderShip.LaserTemp, DashBoardX + 32, DashBoardY + 78, Color.Red);

                // Speed
                display_dial_bar(CommanderShip.Speed, DashBoardX + 418, DashBoardY + 7, Color.Green);

                // Fuel
                display_dial_bar(CommanderShip.Fuel, DashBoardX + 32, DashBoardY + 44, ColorHelper.FromArgb(255, 252, 103, 7));

                // Energy
                if (CommanderShip.arEnergy[3] > 0)
                    display_dial_bar(CommanderShip.arEnergy[3], DashBoardX + 415, DashBoardY + 63, Color.Green);
                if (CommanderShip.arEnergy[2] > 0)
                    display_dial_bar(CommanderShip.arEnergy[2], DashBoardX + 415, DashBoardY + 78, Color.Green);
                if (CommanderShip.arEnergy[1] > 0)
                    display_dial_bar(CommanderShip.arEnergy[1], DashBoardX + 415, DashBoardY + 95, Color.YellowGreen);
                if (CommanderShip.arEnergy[0] > 0)
                    display_dial_bar(CommanderShip.arEnergy[0], DashBoardX + 415, DashBoardY + 113, Color.Yellow);

                //Altitude
                display_dial_bar(CommanderShip.Altitude, DashBoardX + 32, DashBoardY + 95, Color.Red);

                if (CommanderShip.AllowDocking)
                    InSpaceUI.RenderOnScreen(new Rectangle(DashBoardX + 383, DashBoardY + 108, 17, 17), new Rectangle(97, 0, 17, 17));

                
                InSpaceUI.RenderOnScreen(new Rectangle(DashBoardX + 384 + CommanderShip.compass.X,
                    DashBoardY + 25 + CommanderShip.compass.Y, 10, 10), new Rectangle(97, 16, 20, 20), CommanderShip.compass.color);

                // -X - target from left , +X target from right 
                if (CommanderShip.ShowTrackCompass)
                {
                    InSpaceUI.RenderOnScreen(new Rectangle(crossChairX + 48 + CommanderShip.TrackShipCompass.X * 2,
                        crossChairY + 41 + CommanderShip.TrackShipCompass.Y * 3, 10, 10), new Rectangle(97, 16, 20, 20), CommanderShip.compass.color);
                }
            }
        }

        private void display_dial_bar(int len, int x, int y, Color colour)
        {
            for (byte j = 0; j < len; j += 2)
            {
                BaseGame.DrawLine(new Point(x + (int)j, y), new Point(x + (int)j, y + 6), colour);
            }
        }
        #endregion


        

        public void RenderTextsAndMouseCursor()
		{
			// Show fps
            //if (Input.KeyboardF1JustPressed ||
            //    // Also allow toggeling with gamepad
            //    showFps = !showFps;
			//if (showFps)
			Fonts.WriteText(2, 2, "Fps: "+BaseGame.Fps);

			// Render font texts
			//RenderTimeFadeupEffects();
			font.WriteAll();

			// Render mouse
			// For the xbox, there is no mouse support, don't show cursor!
			if (InputManager.MouseDetected &&
				// Also don't show cursor in game!
				SpaceWolf.GameLogic.GameManager.ShowMouseCursor)
            {
            //    // Use our SpriteHelper logic to render the mouse cursor now!
                mouseCursor.RenderOnScreen(InputManager.MousePos);
                SpriteHelper.DrawAllSprites();
            } // if (Input.MouseDetected)
		} // RenderTextsAndMouseCursor()
        
        /// <summary>
        /// Render all UI elements at the end of the frame, will also
        /// render the mouse cursor if we got a mouse attached.
        /// 
        /// Render all ui elements that we collected this frame.
        /// Flush user interface graphics, this are mainly all
        /// Texture.RenderOnScreen calls we made this frame so far.
        /// Used in UIRenderer.RenderTextsAndMouseCursor, but can also be
        /// called to force rendering UI at a specific point in the code.
        /// </summary>
        public static void Render(LineManager2D lineManager2D)
        {
            if (lineManager2D == null)
                throw new ArgumentNullException("lineManager2D");

            // Disable depth buffer for UI
            BaseGame.Device.RenderState.DepthBufferEnable = false;
            // Draw all sprites
            SpriteHelper.DrawAllSprites();

            // Render all 2d lines
            lineManager2D.Render();
        } // Render()
    }
}
